package core.network.client2game.handler.club;

import business.global.club.Club;
import business.global.club.ClubMgr;
import business.global.union.UnionMgr;
import business.player.Player;
import com.ddm.server.websocket.def.ErrorCode;
import com.ddm.server.websocket.exception.WSException;
import com.ddm.server.websocket.handler.requset.WebSocketRequest;
import com.google.gson.Gson;
import core.network.client2game.handler.PlayerHandler;
import core.network.http.proto.SData_Result;
import jsproto.c2s.cclass.club.ClubInfo;
import jsproto.c2s.iclass.club.CClub_Create;
import jsproto.c2s.iclass.union.CUnion_Create;

import java.io.IOException;

/**
 * 创建亲友圈和赛事
 * 
 * @author zaf
 *
 */
public class CClubAndUnionCreate extends PlayerHandler {

	@SuppressWarnings("rawtypes")
	@Override
	public void handle(Player player, WebSocketRequest request, String message) throws WSException, IOException {

		final CClub_Create req = new Gson().fromJson(message, CClub_Create.class);
		SData_Result resultClub = player.checkIdCardAgeValid();
		if (!ErrorCode.Success.equals(resultClub.getCode())) {
			request.error(resultClub.getCode(), resultClub.getMsg());
			return;
		}
		resultClub = ClubMgr.getInstance().getClubListMgr().onClubCreate(req, player,req.getCityId());
		if (!ErrorCode.Success.equals(resultClub.getCode())) {
			request.error(resultClub.getCode(), resultClub.getMsg());
		} else {
			ClubInfo clubInfo = (ClubInfo) resultClub.getData();

			CUnion_Create reqUnion = new Gson().fromJson(message, CUnion_Create.class);
			reqUnion.setUnionName(req.getClubName());
			Club club = ClubMgr.getInstance().getClubListMgr().findClub(clubInfo.getId());
			SData_Result resultUnion = UnionMgr.getInstance().getUnionListMgr().onUnionCreateTest(reqUnion, club, player, club.getClubListBO().getCityId());
			if (!ErrorCode.Success.equals(resultUnion.getCode())) {
				request.error(resultUnion.getCode(), resultUnion.getMsg());
			} else {
				request.response(clubInfo);
			}



		}
	}

}
